package cn.com.joycode.nimble.opencv.test.javacv;

import cn.com.joycode.nimble.opencv.test.BaseTest;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Rect;
import org.bytedeco.opencv.opencv_core.Scalar;
import org.bytedeco.opencv.opencv_java;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.opencv.core.CvType;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;


@Slf4j
public class JavaCVImageRead extends BaseTest {

    @BeforeAll
    public static void before() {
        //System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Loader.load(opencv_java.class);
    }

    @Test
    public void imreadTest() throws Exception {
        String filepath = ResourceUtil.getResource("images/douyin/samples/540/0001.png").getPath();
        //filepath = StrUtil.removePrefix(filepath, "/");
        log.info("读取文件:{}", filepath);
        Mat mat = opencv_imgcodecs.imread(filepath, opencv_imgcodecs.IMREAD_COLOR);
        show(mat);
        /*
        opencv_highgui.imshow("sample", mat);
        opencv_highgui.waitKey();
         */
    }

    @Test
    public void imreadTest1() throws Exception {
        String filepath = ResourceUtil.getResource("images/douyin/samples/540/0001.png").getPath();
        //filepath = StrUtil.removePrefix(filepath, "/");
        log.info("读取文件:{}", filepath);
        Mat mat = opencv_imgcodecs.imread(filepath, opencv_imgcodecs.IMREAD_REDUCED_GRAYSCALE_2);
        //opencv_highgui.imshow("sample", mat);
        //opencv_highgui.waitKey();
        show(mat);
    }

    /**
     * javacv 读取中文路径并显示
     */
    @Test
    public void imreadTest2() throws Exception {
        String filepath = URLUtil.decode(ResourceUtil.getResource("images/douyin/samples/中文测试.png").getPath());
        log.info("读取文件:{}", filepath);
        BufferedImage bufferedImage = ImageIO.read(new File(filepath));
        OpenCVFrameConverter.ToMat openCVConverter = new OpenCVFrameConverter.ToMat();
        Java2DFrameConverter java2DConverter = new Java2DFrameConverter();
        Mat mat = openCVConverter.convert(java2DConverter.convert(bufferedImage));
        int width = 45;
        int height = 20;
        Mat sub = new Mat(width, height, CvType.CV_8UC3);
        Mat tmp = new Mat(mat, new Rect(470, 302, 45, 20));
        tmp.copyTo(sub);
        //
        opencv_imgproc.circle(sub, // 目标图像
                new Point(50, 50), // 圆心坐标
                5, // radius
                new Scalar(0, 255, 0, 1), // 颜色，绿色
                3, // 线宽
                8, // 8-connected line
                0); // shift
        show(tmp);
        /*
        opencv_highgui.imshow("sample", mat);
        opencv_highgui.imshow("sub", sub);
        opencv_highgui.waitKey();*/
    }

    /**
     * javacv 读取中文路径并显示
     */
    @Test
    public void imreadTest3() throws Exception {
        String filepath = URLUtil.decode(ResourceUtil.getResource("images/douyin/samples/中文测试.png").getPath());
        log.info("读取文件:{}", filepath);
        BufferedImage bufferedImage = ImageIO.read(new File(filepath));
        OpenCVFrameConverter.ToMat openCVConverter = new OpenCVFrameConverter.ToMat();
        Java2DFrameConverter java2DConverter = new Java2DFrameConverter();
        Mat mat = openCVConverter.convert(java2DConverter.convert(bufferedImage));
        CanvasFrame canvas = new CanvasFrame("中文测试", 1);
        canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvas.showImage(openCVConverter.convert(mat));
        canvas.waitKey();
    }


}